import { NextRequest, NextResponse } from 'next/server'

// FastGPT会话详情接口地址
const FASTGPT_HISTORY_DETAIL_URL = 'https://fastgpt2.vertu.cn/api/core/chat/getPaginationRecords'

export async function POST(request: NextRequest) {
  try {
    const body = await request.json()

    console.log('🚀 获取会话详情:', { chatId: body.chatId })

    // 转发请求到FastGPT
    const response = await fetch(FASTGPT_HISTORY_DETAIL_URL, {
      method: 'POST',
      headers: {
        'Authorization': `Bearer fastgpt-jtu0gvLwjdc9DRnDvIUZ8SakESKkvG9rkplB0MsarrYMOubtMCU5e`,
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(body)
    })

    if (!response.ok) {
      console.error('❌ FastGPT会话详情接口请求失败:', {
        status: response.status,
        statusText: response.statusText,
      })

      return NextResponse.json(
        { error: '获取会话详情失败', details: await response.text() },
        { status: response.status }
      )
    }

    const data = await response.json()
    console.log('✅ 会话详情获取成功:', { messageCount: data.data?.list?.length || 0 })

    return NextResponse.json(data)

  } catch (error) {
    console.error('❌ 会话详情API转发失败:', error)
    return NextResponse.json(
      { error: '内部服务器错误', message: error instanceof Error ? error.message : '未知错误' },
      { status: 500 }
    )
  }
}
